//
//  BaseSQLite.h
//  ChartRefactor
//
//  Created by CaoJun on 15/4/1.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#ifndef __Chart__BaseSQLite__
#define __Chart__BaseSQLite__

#include "YTXTime.h"
#include "Quotation.h"
#include <sqlite3.h>

#define ContinueResult(ret, error) \
if (ret != SQLITE_OK){ \
NSLog(@"DB Error: %@ %@", [NSString stringWithUTF8String:ConvertToString(ret).c_str() ], [NSString stringWithUTF8String:error]); \
}\

#define CheckResult(ret, error) \
if (ret != SQLITE_OK){ \
NSLog(@"DB Error: %@ %@", [NSString stringWithUTF8String:ConvertToString(ret).c_str() ], [NSString stringWithUTF8String:error]); \
sqlite3_close(sqliteDB); \
return @[]; \
}\

#define CheckDBResult(dbResult, info) \
if(dbResult.status == false){ \
NSLog(@"DBResult False: %@", [NSString stringWithUTF8String:info]); \
sqlite3_close(sqliteDB); \
return @[]; \
} \

namespace ytx {
    
    struct DBResult {
        bool status;
        std::vector<std::vector <std::string> > row_value;
        std::vector<std::vector <std::string> > row_name;
        
        std::string getValue( unsigned int row = 0, unsigned int column = 0){
            if( check(row, column) ){
                return row_value[row][column];
            }
            return NULL;
        }
        
        std::string getName( unsigned int row = 0, unsigned int column = 0){
            if( check(row, column) ){
                return row_name[row][column];
            }
            return NULL;
        }
        
    private:
        bool check(unsigned int row = 0, unsigned int column = 0){
            return status && row < row_value.size() && column < row_value[row].size();
        }
    };
    
    class BaseSQLite
    {
    protected:
        std::string path;
        sqlite3 * sqliteDB;
        static int handelr_exec(void *params,int n_column,char **column_value,char **column_name);
    public:
        const static std::string getTableName(const Quotation * quotation);
        BaseSQLite();
        virtual ~BaseSQLite();
        bool clearTable(std::string tableName);
        bool openDB();
        bool closeDB();
        std::string getPath();
    };
}

#endif /* defined(__Chart__BaseSQLite__) */
